/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2021-01-23 17:03:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const Controller = require('egg').Controller
let moment = require('moment');
class ETCController extends Controller {
    constructor(ctx) {
        super(ctx)

        this.addRule = {
            planName: { type: 'string', required: true, allowEmpty: false },
            mapId: { type: 'string', required: true, allowEmpty: false }
        }
        this.listRule = {
            current: { type: 'int', required: true, allowEmpty: false },
            size: { type: 'int', required: true, allowEmpty: false }
        }
        this.updateRule = {
            _id: { type: 'string', required: true, allowEmpty: false },
            planName: { type: 'string', required: true, allowEmpty: false }
        }
        this.removeRule = {
            ids: { type: 'string', required: true, allowEmpty: false },
        }
        this.detailRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }

    }

    async add() {
        const { ctx } = this
        ctx.validate(this.addRule)
        let { planName, mapId } = ctx.request.body
        let selectMap = await this.ctx.model.BaseSegmentAndPoinManagertModel.findOne({ '_id': mapId })
        if (!selectMap) {
            ctx.helper.InvaidRequest({ ctx, msg: "无该地图id" })
            return
        }
        let plan = await this.ctx.model.PlanManagerModel.create({ planName, mapId })

        ctx.helper.success({ ctx, res: {} })
    }

    async list() {
        const { ctx } = this
        ctx.validate(this.listRule)
        let { current, size } = ctx.request.body
        current = parseInt(current) - 1
        size = parseInt(size)
        let totalCount = await this.ctx.model.PlanManagerModel.find({}).count()
        let managerSces = await this.ctx.model.PlanManagerModel.find({}).sort({ createdAt: 1 }).skip(current * size).limit(size)
        ctx.helper.success({ ctx, res: { records: managerSces, total: totalCount } })
    }
    async update() {
        const { ctx } = this
        ctx.validate(this.updateRule)
        let { _id, planName } = ctx.request.body

        let plan = await this.ctx.model.PlanManagerModel.findOneAndUpdate( //4.删除用户上的设备信息
            {
                '_id': _id
            },
            {
                planName
            },
            { upsert: true, new: true }
        )
        ctx.helper.success({ ctx, res: plan })
    }

    async remove() {
        const { ctx } = this
        ctx.validate(this.removeRule)
        let { ids } = ctx.request.body
        ids = ids.split(',')

        let depandInfo = await this.ctx.model.Scene.find({ planId: { $in: ids } })

        if (depandInfo.length != 0) {
            ctx.helper.InvaidRequest({ ctx, msg: "存在被依赖的数据，删除失败！" })
            return
        }

        await this.ctx.model.PlanManagerModel.remove({ _id: { $in: ids } })


        ctx.helper.success({ ctx, res: {} })
    }

    async detail() {
        const { ctx } = this
        ctx.validate(this.detailRule)
        let { id } = ctx.request.body
        let result = await this.ctx.model.PlanManagerModel.findOne({ _id: id })
        ctx.helper.success({ ctx, res: result })
    }
}


module.exports = ETCController